package o;

import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.BufferOverflowException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class dmf implements dmm {
    private final long ˊ;
    private final RandomAccessFile ˎ;
    private final long ॱ;

    public /* synthetic */ dmm ˊ(long j, long j2) {
        return ॱ(j, j2);
    }

    public dmf(RandomAccessFile randomAccessFile) {
        this.ˎ = randomAccessFile;
        this.ˊ = 0;
        this.ॱ = -1;
    }

    public dmf(RandomAccessFile randomAccessFile, long j, long j2) {
        if (j < 0) {
            throw new IndexOutOfBoundsException("offset: " + j2);
        } else if (j2 < 0) {
            throw new IndexOutOfBoundsException("size: " + j2);
        } else {
            this.ˎ = randomAccessFile;
            this.ˊ = j;
            this.ॱ = j2;
        }
    }

    private static void ˋ(long j, long j2, long j3) {
        if (j < 0) {
            throw new IndexOutOfBoundsException("offset: " + j);
        } else if (j2 < 0) {
            throw new IndexOutOfBoundsException("size: " + j2);
        } else if (j > j3) {
            throw new IndexOutOfBoundsException("offset (" + j + ") > source size (" + j3 + ")");
        } else {
            long j4 = j + j2;
            if (j4 < j) {
                throw new IndexOutOfBoundsException("offset (" + j + ") + size (" + j2 + ") overflow");
            } else if (j4 > j3) {
                throw new IndexOutOfBoundsException("offset (" + j + ") + size (" + j2 + ") > source size (" + j3 + ")");
            }
        }
    }

    public long ॱ() {
        if (this.ॱ != -1) {
            return this.ॱ;
        }
        try {
            return this.ˎ.length();
        } catch (IOException e) {
            return 0;
        }
    }

    public dmf ॱ(long j, long j2) {
        long ॱ = ॱ();
        ˋ(j, j2, ॱ);
        return (j == 0 && j2 == ॱ) ? this : new dmf(this.ˎ, this.ˊ + j, j2);
    }

    public void ˋ(long j, int i, ByteBuffer byteBuffer) throws IOException {
        long j2 = j;
        ˋ(j2, (long) i, ॱ());
        if (i != 0) {
            if (i > byteBuffer.remaining()) {
                throw new BufferOverflowException();
            }
            j2 = this.ˊ + j;
            int limit = byteBuffer.limit();
            try {
                byteBuffer.limit(byteBuffer.position() + i);
                FileChannel channel = this.ˎ.getChannel();
                while (i > 0) {
                    int read;
                    synchronized (this.ˎ) {
                        channel.position(j2);
                        read = channel.read(byteBuffer);
                    }
                    j2 += (long) read;
                    i -= read;
                }
            } finally {
                byteBuffer.limit(limit);
            }
        }
    }

    public ByteBuffer ॱ(long j, int i) throws IOException {
        if (i < 0) {
            throw new IndexOutOfBoundsException("size: " + i);
        }
        ByteBuffer allocate = ByteBuffer.allocate(i);
        ˋ(j, i, allocate);
        allocate.flip();
        return allocate;
    }

    public void ˊ(long j, long j2, dme o_dme) throws IOException {
        ˋ(j, j2, ॱ());
        if (j2 != 0) {
            long j3 = this.ˊ + j;
            byte[] bArr = new byte[((int) Math.min(j2, 65536))];
            while (j2 > 0) {
                int min = (int) Math.min(j2, (long) bArr.length);
                synchronized (this.ˎ) {
                    this.ˎ.seek(j3);
                    this.ˎ.readFully(bArr, 0, min);
                }
                o_dme.ˋ(bArr, 0, min);
                j3 += (long) min;
                j2 -= (long) min;
            }
        }
    }
}
